<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="章节名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入章节名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeGmtCreate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="daterangeGmtModified"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:chapter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:chapter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:chapter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:chapter:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chapterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="章节ID" align="center" prop="id" />
      <el-table-column label="章节名称" align="center" prop="title" />
      <el-table-column label="显示排序" align="center" prop="sort" />
      <el-table-column label="创建时间" align="center" prop="gmtCreate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="gmtModified" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtModified, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:chapter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:chapter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程章节对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="章节名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入章节名称" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入显示排序" />
        </el-form-item>
        <el-divider content-position="center">课程视频信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddEduVideo">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteEduVideo">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="eduVideoList" :row-class-name="rowEduVideoIndex" @selection-change="handleEduVideoSelectionChange" ref="eduVideo">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="课程ID" prop="courseId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.courseId" placeholder="请输入课程ID" />
            </template>
          </el-table-column>
          <el-table-column label="节点名称" prop="title" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.title" placeholder="请输入节点名称" />
            </template>
          </el-table-column>
          <el-table-column label="云端视频资源" prop="videoSourceId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.videoSourceId" placeholder="请输入云端视频资源" />
            </template>
          </el-table-column>
          <el-table-column label="原始文件名称" prop="videoOriginalName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.videoOriginalName" placeholder="请输入原始文件名称" />
            </template>
          </el-table-column>
          <el-table-column label="视频时长" prop="duration" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.duration" placeholder="请输入视频时长" />
            </template>
          </el-table-column>
          <el-table-column label="Empty未上传 Transcoding转码中  Normal正常" prop="status" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择Empty未上传 Transcoding转码中  Normal正常">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="gmtCreate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.gmtCreate" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="gmtModified" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.gmtModified" type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChapter, getChapter, delChapter, addChapter, updateChapter } from "@/api/edu/chapter";

export default {
  name: "Chapter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEduVideo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程章节表格数据
      chapterList: [],
      // 课程视频表格数据
      eduVideoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeGmtCreate: [],
      // 更新时间时间范围
      daterangeGmtModified: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        gmtCreate: null,
        gmtModified: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "章节名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "显示排序不能为空", trigger: "blur" }
        ],
        gmtCreate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        gmtModified: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课程章节列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeGmtCreate && '' != this.daterangeGmtCreate) {
        this.queryParams.params["beginGmtCreate"] = this.daterangeGmtCreate[0];
        this.queryParams.params["endGmtCreate"] = this.daterangeGmtCreate[1];
      }
      if (null != this.daterangeGmtModified && '' != this.daterangeGmtModified) {
        this.queryParams.params["beginGmtModified"] = this.daterangeGmtModified[0];
        this.queryParams.params["endGmtModified"] = this.daterangeGmtModified[1];
      }
      listChapter(this.queryParams).then(response => {
        this.chapterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        title: null,
        sort: null,
        gmtCreate: null,
        gmtModified: null
      };
      this.eduVideoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeGmtCreate = [];
      this.daterangeGmtModified = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程章节";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChapter(id).then(response => {
        this.form = response.data;
        this.eduVideoList = response.data.eduVideoList;
        this.open = true;
        this.title = "修改课程章节";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.eduVideoList = this.eduVideoList;
          if (this.form.id != null) {
            updateChapter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChapter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程章节编号为"' + ids + '"的数据项？').then(function() {
        return delChapter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 课程视频序号 */
    rowEduVideoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 课程视频添加按钮操作 */
    handleAddEduVideo() {
      let obj = {};
      obj.courseId = "";
      obj.title = "";
      obj.videoSourceId = "";
      obj.videoOriginalName = "";
      obj.sort = "";
      obj.playCount = "";
      obj.isFree = "";
      obj.duration = "";
      obj.status = "";
      obj.size = "";
      obj.version = "";
      obj.gmtCreate = "";
      obj.gmtModified = "";
      this.eduVideoList.push(obj);
    },
    /** 课程视频删除按钮操作 */
    handleDeleteEduVideo() {
      if (this.checkedEduVideo.length == 0) {
        this.$modal.msgError("请先选择要删除的课程视频数据");
      } else {
        const eduVideoList = this.eduVideoList;
        const checkedEduVideo = this.checkedEduVideo;
        this.eduVideoList = eduVideoList.filter(function(item) {
          return checkedEduVideo.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleEduVideoSelectionChange(selection) {
      this.checkedEduVideo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edu/chapter/export', {
        ...this.queryParams
      }, `chapter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
